// index.js
// 获取应用实例
const app = getApp()
const { axios } = require('../../utils/http')
Page({
  data: {
    active: 0,
    value: '',
    blogList: [],
    filterBlogList: [],
    blogWayArr: ['全部', 'vue', 'react', 'Node.js', 'js', 'css', '随笔']
  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 0
      })
    }
    this.getBlogList()
  },
  changeValue({ detail }) {
    this.setData({
      filterBlogList: detail ? this.data.blogList.filter(item => item.title.indexOf(detail) !== -1) : this.data.blogList
    })
  },
  changeTab(event) {
    const { title } = event.detail
    this.setData({
      filterBlogList: title === '全部' ? this.data.blogList : this.data.blogList.filter(item => item.way === title)
    })
  },
  getBlogList() {
    axios.get('/blog/alllist').then(res => {
      console.log(res)
      this.setData({
        blogList: res.data.data,
        filterBlogList: res.data.data
      })
    })
  },
  goBlogDetail(item) {
    console.log(item)
    const { id } = item.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/blogDetail/blogDetail?id=' + id
    })
  }
})
